package cn.yshujia.handler;

import cn.yshujia.config.SocketIOConfig;
import cn.yshujia.domain.entity.Comment;
import cn.yshujia.domain.vo.CommentVO;
import cn.yshujia.service.impl.CommentServiceImpl;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.fasterxml.jackson.core.JsonProcessingException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author YShuJia
 * @create 2024/12/16
 * @description CommentSocketIOHandler 评论模块
 */

@Slf4j
@Component
public class CommentSocketIOHandler {
	
	@Resource
	private SocketIOServer socketIoServer;
	
	@Resource
	private SocketIOConfig socketIOConfig;
	
	@Resource
	private CommentServiceImpl commentService;
	
	@OnEvent ("commentSocket")
	public void commentHandler(SocketIOClient client, Comment dto, AckRequest ackRequest) throws JsonProcessingException {
		
		Long commentId = commentService.insert(dto);
		CommentVO vo = commentService.selectById(commentId);
		if (null != vo) {
			// 广播评论
			socketIoServer.getNamespace("/comment")
					.getBroadcastOperations()
					.sendEvent("commentMessage", vo);
		}
	}
}
